package com.lzy.cloud.permission.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.cloud.permission.dtos.ResourceDto;
import com.lzy.cloud.permission.dtos.RoleDto;
import com.lzy.cloud.permission.entity.Resource;
import com.lzy.cloud.permission.mapper.ResourceMapperExt;
import com.lzy.cloud.permission.service.ResourceService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.util.Resources_de;

import java.util.List;
import java.util.Map;

/**
 * Created by WIN7 on 2018/5/16.
 */
@Service
public class ResourceServiceImpl implements ResourceService{

    @Autowired
    ResourceMapperExt resourceMapperExt;

    @Override
    public List<ResourceDto> selectAllResource(ResourceDto resourceDto) {
        return resourceMapperExt.selectList(resourceDto);
    }

    @Override
    public ResourceDto selectByPrimaryKey(Long id) {
        return DozerUtils.map(resourceMapperExt.selectByPrimaryKey(id),ResourceDto.class);
    }

    @Override
    public PageInfo<ResourceDto> queryResourceByParentId(Pager pager, ResourceDto resourceDto) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        List<ResourceDto> resourceDtoList = resourceMapperExt.selectList(resourceDto);
        return new PageInfo(resourceDtoList);
    }

    @Override
    public Boolean insert(ResourceDto resourceDto) {
        if(resourceDto != null){
            if(resourceDto.getUrl() != null){
                ResourceDto dto = checkUrl(resourceDto.getUrl());
                Ensure.that(dto).isNull("00030007");
            }
        }
        return resourceMapperExt.insertSelective(DozerUtils.map(resourceDto, Resource.class))>0;
    }

    @Override
    public Boolean update(ResourceDto resourceDto) {
        if(resourceDto != null){
            if(resourceDto.getUrl() != null){
                ResourceDto dto = checkUrl(resourceDto.getUrl());
                Ensure.that(dto).isNull("00030007");
            }
        }
        return resourceMapperExt.updateByPrimaryKeySelective(DozerUtils.map(resourceDto,Resource.class))>0;
    }

    @Override
    public Boolean deleteById(Long id) {
        return resourceMapperExt.deleteByPrimaryKey(id)>0;
    }

    @Override
    public ResourceDto selectResource(ResourceDto resourceDto) {
        return resourceMapperExt.selectResource(resourceDto);
    }

    @Override
    public List<ResourceDto> selectList(ResourceDto resourceDto) {
        return resourceMapperExt.selectList(resourceDto);
    }

    @Override
    public List<ResourceDto> selectByIds(Map<String, Object> params) {
        return resourceMapperExt.selectByIds(params);
    }

    @Override
    public Integer countByIds(Map<String, Object> map) {
        return resourceMapperExt.countByIds(map);
    }

    /**
     * 资源连接地址判重
     * @param url
     * @return
     */
    private ResourceDto checkUrl(String url){
        ResourceDto resource = new ResourceDto();
        resource.setUrl(resource.getUrl());
        return resourceMapperExt.selectResource(resource);
    }
}
